package com.wynn;

import com.wynn.dao.PlanDao;
import com.wynn.dao.ProductDao;
import com.wynn.pojo.Plan;
import com.wynn.pojo.PlanSummary;
import com.wynn.pojo.Product;
import com.wynn.pojo.ProductLine;
import org.apache.ibatis.annotations.Insert;
import org.junit.jupiter.api.Test;
import org.python.core.PyFunction;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.PortUnreachableException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@SpringBootTest(classes = MESServiceBaseApplication.class)
public class test {


    @Resource
    private PlanDao planDao;
    @Resource
    private ProductDao productDao;

    @Value("${planCalculate.addr}")
    private String addr;


    @Test
    public void test(){
        System.out.println(addr);
        List<PlanSummary> summary = planDao.findSummary();
        for (PlanSummary planSummary : summary) {
            System.out.println(
                    planSummary.getId()
            );
        }
    }

    @Test
    public void test01(){
        List<Integer> num = new ArrayList<>();

        List<Product> allProduct = productDao.findAllProduct();
        //按时间和产线分组查询
        List<PlanSummary> plans  = planDao.findSummary();
        for (PlanSummary plan : plans) {
            for (Product product : allProduct) {
                Integer tpaln = planDao.findProductNum(plan.getProduceDate(),plan.getProductLine(),product);
                System.out.println(tpaln);
            }
        }

    }

    @Test
    public void plan() throws IOException {
        //原始数据
        List<Integer> word = new ArrayList<>(Arrays.asList(8,5,10,3,3,3));

        String pythonFilePath = "D:\\web_develop\\idea\\work\\ERP\\MES_parent\\MES_service_provider_base\\src\\main\\resources\\dog.py";
        String pythonFileMethod = "start";

        PythonInterpreter interpreter = new PythonInterpreter();
//        ClassPathResource resource = new ClassPathResource(pythonFilePath);
//        InputStream inputStream = resource.getInputStream();
        interpreter.execfile(pythonFilePath);
        PyFunction verify = interpreter.get(pythonFileMethod, PyFunction.class);
        //调用
        PyObject pyObject = verify.__call__(new PyList(word), new PyList(word));
        List<String> result = (List<String>)pyObject.__tojava__(List.class);
        System.out.println(result);

    }

    @Test
    public void Jython() throws IOException {

        try{
            String result = "";
            int[] a={8,5,10,3,3,3};
            List<Integer> integers = Arrays.asList(0, 0, 32, 0, 0);
            List<String> lab = Arrays.asList("a","a","a","a","a");
            int tt=35;
            String[] arguments = new String[] {"python", "D:\\python_work\\work\\dog.py",integers.toString(),"'a','a','ab','a','a'",Integer.toString(tt)};
            //读取到python文件
            Process pr = Runtime.getRuntime().exec(arguments );
            InputStreamReader ir = new InputStreamReader(pr.getInputStream());
            LineNumberReader in = new LineNumberReader(ir);

            result += in.readLine();
            System.out.println("result"+result);
            int length = result.length();

                System.out.println(result.substring(1,length-1));
            String[] split = result.substring(1,length-1).split(", ");
            for (String s : split) {
                System.out.println(s.substring(1,s.length()-1));
            }

//            String line ;
//            //获取到python中的所有print 数据，
//            while((line=in.readLine()) != null){
//                System.out.println("python print data"+line);}
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    @Test
    public void convertDate() throws ParseException {
        String s = "2022-07-16T06:52:38.731Z";
        Date date1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(s);
        System.out.println(date1);

    }


}
